package com.icss.back.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.icss.common.servlet.BaseServlet;
import com.icss.common.util.PageInfoUtil;
import com.icss.common.vo.ApiResponse;
import com.icss.service.IOrdersDbService;
import com.icss.service.impl.OrdersDbServiceImpl;
import com.icss.vo.MakeupOrders;
import com.icss.vo.MakeupOrdersProd;

/**
 * 订单操作
 * 
 * @author 陈岩
 */
@WebServlet(urlPatterns = "/back/OrdersBackServlet")
public class OrdersBackServlet extends BaseServlet
{
	@Resource
	private IOrdersDbService ordersDbService;

	@Override
	public void init(ServletConfig config) throws ServletException
	{
		super.init(config);
		this.ordersDbService = new OrdersDbServiceImpl();
	}
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/* 接收参数 */
		String method = request.getParameter("method");
		if ("ordersList".equalsIgnoreCase(method))
		{
			/* ordersList */
			ordersList(request, response);
		} else if ("ordersUpdate".equalsIgnoreCase(method))
		{
			/* ordersUpdate */
			ordersUpdate(request, response);
		} else if ("ordersProductList".equalsIgnoreCase(method))
		{
			/* ordersProductList */
			ordersProductList(request, response);
		} else if ("ordersProductUpdate".equalsIgnoreCase(method))
		{
			/* ordersProductUpdate */
			ordersProductUpdate(request, response);
		} 
	}
	
	/**
	 * 订单列表
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public String ordersList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<MakeupOrders> apiResponse = this.ordersDbService.findCondListOrdersService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 页面跳转 */
		return "/back/orders/ordersList";
	}

	/**
	 * 订单更新
	 * @param request
	 * @param response
	 */
	public String ordersUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("id", id);
		ApiResponse<MakeupOrders> apiResponse = this.ordersDbService.findOneOrdersService(condMap);
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 根据不同的判断处理不同的类型 */
		String operType = request.getParameter("operType");
		if("update".equalsIgnoreCase(operType))
		{
			return "/back/orders/ordersUpdate";
		}
		/* 页面跳转 */
		return "/back/orders/ordersInfo";
	}

	/**
	 * 订单商品列表
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public String ordersProductList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		String ordersId = request.getParameter("ordersId");
		if(ordersId == null)
		{
			ordersId = "" ; 
		}
		request.setAttribute("ordersId", ordersId);
		condMap.put("ordersId", ordersId);
		
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<MakeupOrdersProd> apiResponse = this.ordersDbService.findCondListOrdersProductService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 页面跳转 */
		return "/back/orders/ordersProductList";
	}

	/**
	 * 订单商品更新
	 * @param request
	 * @param response
	 */
	public String ordersProductUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("id", id);
		ApiResponse<MakeupOrdersProd> apiResponse = this.ordersDbService.findOneOrdersProductService(condMap);
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 根据不同的判断处理不同的类型 */
		String operType = request.getParameter("operType");
		if("update".equalsIgnoreCase(operType))
		{
			return "/back/orders/ordersProductUpdate";
		}
		/* 页面跳转 */
		return "/back/orders/ordersProductInfo";
	}
}